home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume3 / ff / part02 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  31.6 KB

  1. From: decvax!wanginst!perlman
  2. Subject: ff: fast test formatter (part 2 of 2)
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 52
  7. Submitted by: decvax!wanginst!perlman
  8.  
  9. #! /bin/sh
  10. # This is a shell archive, meaning:
  11. # 1. Remove everything above the #! /bin/sh line.
  12. # 2. Save the resulting text in a file.
  13. # 3. Execute the file with /bin/sh (not csh) to create the files:
  14. #    ff.c
  15. # This archive created: Wed Nov 13 19:28:01 1985
  16. # By:    Gary Perlman (Wang Institute, Tyngsboro, MA 01879 USA)
  17. export PATH; PATH=/bin:/usr/bin:$PATH
  18. echo shar: "extracting 'ff.c'" '(29326 characters)'
  19. if test -f 'ff.c'
  20. then
  21.     echo shar: "will not over-write existing file 'ff.c'"
  22. else
  23. sed 's/^    X//' << \SHAR_EOF > 'ff.c'
  24.     X/*COPYRIGHT (c) 1985 Wang Institute, Tyngsboro, MA 01879 USA */
  25.     X/*DISCLAIMER:    No guarantees of performance are made. */
  26.     X/*MODULE    ff: "fast formatter" "Sun 01 Sep 1985" */
  27.     X/*PGMR      ff: "Gary Perlman" "Wang Institute, Tyngsboro, MA 01879 USA" */
  28.     X/*VER       $Header: ff.c,v 1.3 85/09/01 21:23:38 perlman Exp $ */
  29.     X/*COMP      ff: see makefile */
  30.     X
  31.     X/*MANUAL.TH FF 1 "August 10, 1985" "Wang Institute" "UNIX User's Manual"
  32.     X.\" $Compile: iroff -man.new %f
  33.     X.SH NAME
  34.     Xff \- fast text formatter
  35.     X.SH USAGE
  36.     X.B ff
  37.     X[options] [-] [files]
  38.     X.SH DESCRIPTION
  39.     X.I ff
  40.     Xis a simple text formatter for flexible uniform formatting of
  41.     Xinput files.
  42.     XProgram options are used to control formatting.
  43.     XThis is in contrast to text formatters like
  44.     X.I nroff (1)
  45.     Xthat require special format requests to be part of their input files.
  46.     XBesides avoiding cryptic format requests in text,
  47.     X.I ff
  48.     Xis considerably faster than traditional formatters like
  49.     X.I nroff (1)
  50.     Xand even simple formatters like
  51.     X.I fmt (1).
  52.     X.PP
  53.     XThe most complicated concept with
  54.     X.I ff
  55.     Xis that of a line break.
  56.     XA line break causes an interruption in the filling
  57.     X(evening out of the text lines).
  58.     XLine breaks occur when special characters are seen at the beginnings
  59.     Xof lines, or when all lines are broken.
  60.     XBy default, any non-alphanumeric character will cause a break,
  61.     Xbut this can be controlled with the
  62.     X.B -B
  63.     Xoption.
  64.     XA blank line always causes a break.
  65.     X*/
  66.     X
  67.     X
  68.     X#include <stdio.h>
  69.     X#include <ctype.h>
  70.     X#include <strings.h>
  71.     X
  72.     Xtypedef    int     Status;
  73.     X#define    SUCCESS   ((Status) 0)
  74.     X#define    FAILURE   ((Status) 1)
  75.     Xtypedef    int     Boole;
  76.     X#define    TRUE      ((Boole) 1)
  77.     X#define    FALSE     ((Boole) 0)
  78.     X
  79.     X#define    TAB       '\t'
  80.     X#define    EOL       '\n'
  81.     X#define    FF        '\f'
  82.     X#define    EOS       '\0'
  83.     X#define    SP        ' '
  84.     X#define    MAXLEN    128                  /* max length of lines */
  85.     X#define    MAXLINES  200                  /* max # lines on pages */
  86.     X
  87.     X/* Alphabetical listing of this file's functions */
  88.     Xvoid    beginline ();  /* process text at the beginning of lines */
  89.     Xvoid    beginpage ();  /* handle pagination at page breaks */
  90.     XStatus    dobreak ();    /* handle broken lines, if appropriate */
  91.     Xvoid    dofill ();     /* do the text filling */
  92.     Xchar    *dotab ();     /* return expanded tabs in line */
  93.     Xvoid    endpage ();    /* handle pagination at page ends */
  94.     Xchar    *expand ();    /* expand strings in three part titles */
  95.     XStatus    ff ();         /* main formatting routine */
  96.     Xint     initial ();    /* set options and check consistency */
  97.     Xchar    *itoa ();      /* convert integer to ascii format, with padding */
  98.     Xvoid    dojustify ();  /* even out (justify) the right margin of filled lines */
  99.     Xchar    *preprocess ();/* handle blank trimming and titling */
  100.     Xvoid    println ();    /* print a line, watching for page boundaries */
  101.     Xvoid    repeat ();     /* repeatedly print a character */
  102.     XStatus    setint ();     /* check type & convert a string to an integer */
  103.     Xchar    *threepart (); /* build three part titles */
  104.     Xvoid    usage ();      /* print usage summary */
  105.     X
  106.     X/* GLOBALS */
  107.     Xchar    *Argv0;              /* will be name of program */
  108.     Xint     Curpos;              /* current position on output line */
  109.     Xchar    *Filename;           /* current input file name */
  110.     XBoole    Filling;             /* is text being filled right now */
  111.     Xchar    Justbuf[MAXLEN];     /* buffer for justified text */
  112.     Xint     Justpos;             /* current position in justification buffer */
  113.     Xint     Outline;             /* output line number */
  114.     Xint     Pageline;            /* line number on current output page */
  115.     Xint     Pagenum;             /* current page number */
  116.     X
  117.     X /* Default values of options */
  118.     X#define    MAXTAB     20                  /* max # of tab stops */
  119.     X#define    FOOTSIZE    5                  /* default footer size */
  120.     X#define    HEADSIZE    5                  /* default header size */
  121.     X#define    NUMWIDTH    4                  /* default width of line numbers */
  122.     X#define    PAGESIZE   66                  /* default length of page */
  123.     X#define    WIDTH      72                  /* default width of page */
  124.     X#define    PAGENUM   '%'                  /* expands to page number in titles */
  125.     X#define    FILENAME  '*'                  /* expands to file name in title */
  126.     X#define    HEADER      "|File: *||Page: %|" /* default page header */
  127.     X
  128.     X/*MANUAL.SH OPTIONS
  129.     XThere are many, many options to allow control of
  130.     Xindentation, line width, line spacing, filling,
  131.     Xpagination with headers and footers,
  132.     Xline numbering, right justification,
  133.     Xand perhaps some other things.
  134.     XThey have extensive type and range checking
  135.     Xthat produces diagnostic error messages,
  136.     Xso warnings of obviously wrong options will not be discussed here.
  137.     XIn general, options that imply the use of others
  138.     Xwork the way they should; if the page size is set,
  139.     Xthen pagination is automatically assumed.
  140.     XSome combinations of options give impressive, even artistic, effects.
  141.     XMaking a small text file and playing with it is the easiest
  142.     Xway to learn how the options interact.
  143.     X.de OP
  144.     X.TP
  145.     X.B -\\$1 \\$2
  146.     X..
  147.     X*/
  148.     X
  149.     X    Boole    Breaklines = FALSE; /*MANUAL.OP b
  150.     XBreak all lines of text.
  151.     XThat is, don't even-out lines by filling.
  152.     XBy default, text lines are filled.
  153.     X*/
  154.     X
  155.     X    char    *Breakchars = NULL; /*MANUAL.OP B breakchars
  156.     XChange the set of characters that cause line breaks at the start of lines to
  157.     X.I breakchars.
  158.     XBy default, any characters but letters and numbers cause a break.
  159.     XA good choice for break characters might be "*-+" and TABS
  160.     Xthat might be used for lists.
  161.     X*/
  162.     X
  163.     X    Boole    Center = FALSE; /*MANUAL.OP c
  164.     XCenter all lines of text.
  165.     XThis option stops all filling of text.
  166.     X*/
  167.     X
  168.     X    Boole    Delspace = FALSE; /*MANUAL.OP d
  169.     XDelete white space at the beginning and end of lines.
  170.     XThis option is useful to help un-format text to be re-formatted.
  171.     X*/
  172.     X
  173.     X    Boole    Delline = FALSE; /*MANUAL.OP D
  174.     XDelete empty input lines.
  175.     XAn input line is empty if it has no characters,
  176.     Xnot even invisible character like tabs or spaces.
  177.     XThis option can be combined with the option to remove white space
  178.     Xto delete visibly blank lines.
  179.     X*/
  180.     X
  181.     X    char    *Footer = NULL; /*MANUAL.OP f footer
  182.     XSet the page footer to the string
  183.     X.I footer.
  184.     XThis can be any string,
  185.     Xbut if the first character is not a letter or a digit,
  186.     Xbut a punctuation character like /,
  187.     Xthen that character separates the left,
  188.     Xcenter, and right fields of a title.
  189.     XFor example, the title
  190.     X.ce
  191.     X"/ff: fast formatter//1985/"
  192.     Xwould have "ff: fast formatter" as a left justified field
  193.     Xand 1985 as a right justified field on each page.
  194.     XNote that there is no middle field in this example,
  195.     Xbut there could have been, between the two consecutive /'s.
  196.     XThere are two special characters, % and *,
  197.     Xthat respectively are variables for the page number
  198.     Xand the input file name.
  199.     XThe default page footer is blank.
  200.     X*/
  201.     X
  202.     X    int     Footsize = FOOTSIZE; /*MANUAL.OP F footersize
  203.     XSet the number of blank lines at the bottom of the page.
  204.     XThe footer, if any, is placed in the middle of the space,
  205.     Xwhich by default, is five lines.
  206.     XIf
  207.     X.I footersize
  208.     Xis 0, no footer will be printed.
  209.     X*/
  210.     X
  211.     X    char    *Header = HEADER; /*MANUAL.OP h header
  212.     XSet the page header.
  213.     XSee the description of three-part titles for the
  214.     X.B -f footer
  215.     Xoption.
  216.     XThe default page header is
  217.     X.ce
  218.     X"|File: *||Page: %|".
  219.     X*/
  220.     X
  221.     X    int     Headsize = HEADSIZE; /*MANUAL.OP H headersize
  222.     XSee the description of the footer size.
  223.     X*/
  224.     X
  225.     X    int     Indent = 0; /*MANUAL.OP i indent
  226.     XSet the indentation of the text to
  227.     X.I indent
  228.     Xspaces.
  229.     XNote that this effectively reduces the usable width of the page.
  230.     X*/
  231.     X
  232.     X    int     Tindent = 0; /*MANUAL.OP I tempindent
  233.     XSet the temporary indent.
  234.     XThis causes filled text found immediately after a break to
  235.     Xbe indented for one line.
  236.     XIt is useful for indenting the first lines of paragraphs.
  237.     XIf
  238.     X.I tempindent
  239.     Xis negative,
  240.     Xthe the temporary indent will be relative to the current
  241.     X.I indent
  242.     Xvalue.
  243.     XIf the
  244.     X.I tempindent
  245.     Xvalue is more negative than the
  246.     X.I indent
  247.     Xvalue is positive,
  248.     X.I ff
  249.     Xwill automatically increase
  250.     X.I indent.
  251.     X*/
  252.     X
  253.     X    Boole    Justify = FALSE; /*MANUAL.OP j
  254.     XJustify the text.
  255.     XThat is, even the right margin by inserting spaces in the line.
  256.     XOnly filled text can be justified.
  257.     X*/
  258.     X
  259.     X    Boole    Numlines = FALSE; /*MANUAL.OP n
  260.     XNumber all output lines produced by the input text.
  261.     XLines from multiple line spacing or pagination will not
  262.     Xbe numbered.
  263.     X*/
  264.     X
  265.     X    int     Numwidth = NUMWIDTH; /*MANUAL.OP N numberwidth
  266.     XSet the width of the line numbers.
  267.     XThe default is to take up 4 spaces.
  268.     XNote that this effectively reduces the usable part of the page.
  269.     X*/
  270.     X
  271.     X    Boole    Paginate = FALSE; /*MANUAL.OP p
  272.     XPaginate the output.
  273.     XSee the options for page header and footer control.
  274.     X*/
  275.     X
  276.     X    int     Pagesize = PAGESIZE; /*MANUAL.OP P pagesize
  277.     XSet the number of lines in a page to
  278.     X.I pagesize.
  279.     XBy default, the standard 66 line page is assumed.
  280.     X*/
  281.     X
  282.     X    int     Spacing = 1; /*MANUAL.OP s spacing
  283.     XSet the line spacing.
  284.     XBy default, text is single spaced (\fIspacing\fR equals 1).
  285.     X*/
  286.     X
  287.     X    int     Tab[MAXTAB];
  288.     X    int     Ntabs = 0; /*MANUAL.OP t tab
  289.     XSet individual absolute and relative tab stops.
  290.     XThese values tell the formatter
  291.     Xwhere to put the text (from an unfilled line)
  292.     Xthat follows a tab character.
  293.     XEach tab stop is supplied with its own
  294.     X.B -t
  295.     Xoption; there is no way to bundle them.
  296.     X.I tab
  297.     Xvalues can be integers without a plus sign.
  298.     XThese are \fIabsolute\fR tab settings;
  299.     Xthe tabs go to that position.
  300.     XThe values must increase monotonically.
  301.     XIf a
  302.     X.I tab
  303.     Xvalue is preceded by a plus sign,
  304.     Xthen it is interpreted \fIrelative\fR to the previous tab setting.
  305.     XFor example, a tab setting of 40 followed by one of +20
  306.     Xwill set the second tab stop to 60.
  307.     X*/
  308.     X
  309.     X    int     Alltabs = 0; /*MANUAL.OP T tabs
  310.     XSet tab stops to every
  311.     X.I tabs
  312.     Xspaces.
  313.     XIt is useful to get equally spaced tabs.
  314.     XThis option cannot be used with the other tab setting option.
  315.     X*/
  316.     X
  317.     X    Boole    Uppercase = FALSE; /*MANUAL.OP u
  318.     XPrint All Input Text As Initial Upper-Case Titles,
  319.     XLike This Sentence.
  320.     XThis option goes well with the one for centering lines.
  321.     X*/
  322.     X
  323.     X    /*MANUAL.OP U
  324.     XPrint a usage summary of all the options and stop.
  325.     X*/
  326.     X
  327.     X    int     Width = WIDTH; /*MANUAL.OP w width
  328.     XSet the page width.
  329.     XBy default, the page width is 72 characters.
  330.     XNote that the usable line length is sometimes less
  331.     Xthan the page width.
  332.     XIf line numbering or indentation is requested,
  333.     Xthese subtract from the line length.
  334.     X*/
  335.     X
  336.     X /*MANUAL.SH EXAMPLES
  337.     XSome of these examples can make shell scripts or aliases.
  338.     X.nf
  339.     X.ta .5i
  340.     X.sp
  341.     XCentered Titles: title
  342.     X    ff -dcu $*
  343.     XDouble Spaced Unfilled Paginated indented (for editing): draft
  344.     X    ff -s 2 -b -p -f "`date`" -i 8 $*
  345.     XProgram Listing: cpr
  346.     X    H="@        Dir: `pwd`@@File: *@"
  347.     X    F="@        $NAME@`date`@Page %@"
  348.     X    ff -b -N 8 -H 3 -h "$H" -F 3 -f "$F" -T 4 -w 79 -i 2 $*
  349.     XReformat Paragraphed Text: nr
  350.     X    ff -jd -I 5 -i 10 -w 65 -B "TAB SP'*.@" $*
  351.     X.fi
  352.     X*/
  353.     X/*MANUAL.SH DIAGNOSTICS
  354.     XSome options are incompatible with others.
  355.     XFor example, centered text cannot be right-justified.
  356.     X.I ff
  357.     Xwill not allow inconsistent combinations of options.
  358.     X*/
  359.     X/*MANUAL.SH "SEE ALSO"
  360.     Xfmt(1), nroff(1), scribe(1w)
  361.     X*/
  362.     X/*MANUAL.SH AUTHOR
  363.     XGary Perlman (with help from many students)
  364.     X*/
  365.     X/*MANUAL.SH STATUS
  366.     XPretty well tested.
  367.     X*/
  368.     X
  369.     X /* Macro Functions */
  370.     X/*MACRO isendsent: is the character one that ends a sentence? */
  371.     X#define    isendsent(c) ((c) == '.' || (c) == '?' || (c) == '!')
  372.     X
  373.     X/*MACRO justchar: add char to a buffer that will later be flushed */
  374.     X#define    justchar(c) (Justbuf[Justpos++] = (c))
  375.     X
  376.     X/*MACRO fillchar: save to justify if necessary, else output */
  377.     X#define fillchar(c) \
  378.     X    { \
  379.     X    if (Justify == TRUE) justchar (c); \
  380.     X    else putc (c, stdout); \
  381.     X    }
  382.     X
  383.     X /*FUNCTION main: loop through files in classic UNIX filter style */
  384.     Xmain (argc, argv)
  385.     Xint     argc;     /* argument count */
  386.     Xchar    **argv;   /* argument vector */
  387.     X    {
  388.     X    Status     ff ();      /* ff (file, ioptr) will filter files */
  389.     X    Status    status;     /* return status of filter () */
  390.     X    int     firstfile;  /* first file name index returned by initial */
  391.     X
  392.     X    firstfile = initial (argc, argv);
  393.     X    status = filter (argc, argv, firstfile, ff);
  394.     X    exit (status);
  395.     X    }
  396.     X
  397.     X /*FUNCTION setint: check type, convert string, and set integer option */
  398.     XStatus
  399.     Xsetint (flag, value, var, min, max)
  400.     Xint     flag;    /* the single character option name */
  401.     Xchar    *value;  /* the candidate value, in string format */
  402.     Xint     *var;    /* ptr to variable to stuff in answer */
  403.     Xint     min;     /* minimum allowed value */
  404.     Xint     max;     /* maximum allowed value */
  405.     X    {
  406.     X    int     tmpvar;
  407.     X    if (number (value) == 1) /* number returns 1 for integers, 2 for reals */
  408.     X        {
  409.     X        tmpvar = atoi (value);
  410.     X        if (tmpvar >= min && tmpvar <= max)
  411.     X            {
  412.     X            *var = tmpvar;
  413.     X            return (SUCCESS);
  414.     X            }
  415.     X        fprintf (stderr, "%s: -%c option value must be between %d and %d\n",
  416.     X            Argv0, flag, min, max);
  417.     X        return (FAILURE);
  418.     X        }
  419.     X    fprintf (stderr, "%s: -%c option requires an integer value\n", Argv0, flag);
  420.     X    return (FAILURE);
  421.     X    }
  422.     X
  423.     X /*FUNCTION usage: print help menu */
  424.     Xvoid
  425.     Xusage (ioptr)
  426.     XFILE    *ioptr;
  427.     X    {
  428.     X    fprintf (ioptr, "%s: fast text formatter options:\n", Argv0);
  429.     X    fprintf (ioptr, "-b      break all lines of text--do no filling\n");
  430.     X    fprintf (ioptr, "-B s    line break characters\n");
  431.     X    fprintf (ioptr, "-c      center all text lines\n");
  432.     X    fprintf (ioptr, "-d      delete blank space around input lines\n");
  433.     X    fprintf (ioptr, "-D      delete blank input lines\n");
  434.     X    fprintf (ioptr, "-f s    page footer three-part title\n");
  435.     X    fprintf (ioptr, "-F i    page footer size (%d lines)\n", Footsize);
  436.     X    fprintf (ioptr, "-h s    page header three-part title (%s)\n", Header);
  437.     X    fprintf (ioptr, "-H i    page header size (%d lines)\n", Headsize);
  438.     X    fprintf (ioptr, "-i i    text indentation (%d spaces)\n", Indent);
  439.     X    fprintf (ioptr, "-I i    indent after line breaks (%d spaces)\n", Tindent);
  440.     X    fprintf (ioptr, "-j      justify the right margin of the text\n");
  441.     X    fprintf (ioptr, "-n      number output text lines\n");
  442.     X    fprintf (ioptr, "-N i    width of line numbers (%d spaces)\n", Numwidth);
  443.     X    fprintf (ioptr, "-p      paginate output\n");
  444.     X    fprintf (ioptr, "-P i    page size (%d lines)\n", Pagesize);
  445.     X    fprintf (ioptr, "-s i    line spacing (%d line)\n", Spacing);
  446.     X    fprintf (ioptr, "-t i    absolute or relative tab stop\n");
  447.     X    fprintf (ioptr, "-T i    uniform tab stops\n");
  448.     X    fprintf (ioptr, "-u      show text with upper-case initial letters\n");
  449.     X    fprintf (ioptr, "-U      print long usage synopsis for this command\n");
  450.     X    fprintf (ioptr, "-w i    page line width (%d spaces)\n", Width);
  451.     X    }
  452.     X
  453.     X /*FUNCTION initial: set options */
  454.     Xint
  455.     Xinitial (argc, argv) char **argv;
  456.     X    {
  457.     X    extern    char    *optarg;   /* string value to option set by getopt */
  458.     X    extern    int     optind;    /* will be index of first command operand */
  459.     X    int     errcount = 0;        /* count of number of errors */
  460.     X    int     flag;              /* options flag names read in here */
  461.     X    char    *optstring =       /* Boolean flags and integer options with : */
  462.     X        "bcdDjnpuUB:f:F:h:H:i:I:N:P:s:t:T:w:";
  463.     X
  464.     X    Argv0 = argv[0];
  465.     X    while ((flag = getopt (argc, argv, optstring)) != EOF)
  466.     X        switch (flag)
  467.     X        {
  468.     X        default:
  469.     X            errcount++;
  470.     X            break;
  471.     X
  472.     X        case 'b':
  473.     X            Breaklines = TRUE;
  474.     X            break;
  475.     X
  476.     X        case 'B':
  477.     X            Breakchars = optarg;
  478.     X            break;
  479.     X
  480.     X        case 'c':
  481.     X            Center = TRUE;
  482.     X            Breaklines = TRUE;
  483.     X            break;
  484.     X
  485.     X        case 'd':
  486.     X            Delspace = TRUE;
  487.     X            break;
  488.     X
  489.     X        case 'D':
  490.     X            Delline = TRUE;
  491.     X            break;
  492.     X
  493.     X        case 'f':
  494.     X            Footer = optarg;
  495.     X            Paginate = TRUE;
  496.     X            break;
  497.     X
  498.     X        case 'F':
  499.     X            if (setint (flag, optarg, &Footsize, 0, MAXLINES) == FAILURE)
  500.     X                errcount++;
  501.     X            Paginate = TRUE;
  502.     X            break;
  503.     X
  504.     X        case 'h':
  505.     X            Header = optarg;
  506.     X            Paginate = TRUE;
  507.     X            break;
  508.     X
  509.     X        case 'H':
  510.     X            if (setint (flag, optarg, &Headsize, 0, MAXLINES) == FAILURE)
  511.     X                errcount++;
  512.     X            Paginate = TRUE;
  513.     X            break;
  514.     X
  515.     X        case 'i':
  516.     X            if (setint (flag, optarg, &Indent, 0, MAXLEN) == FAILURE)
  517.     X                errcount++;
  518.     X            break;
  519.     X
  520.     X        case 'I':
  521.     X            if (setint (flag, optarg, &Tindent, -MAXLEN, MAXLEN) == FAILURE)
  522.     X                errcount++;
  523.     X            break;
  524.     X
  525.     X        case 'j':
  526.     X            Justify = TRUE;
  527.     X            break;
  528.     X
  529.     X        case 'N':
  530.     X            if (setint (flag, optarg, &Numwidth, 1, MAXLEN) == FAILURE)
  531.     X                errcount++;
  532.     X            /* FALLTHROUGH */
  533.     X
  534.     X        case 'n':
  535.     X            Numlines = TRUE;
  536.     X            break;
  537.     X
  538.     X        case 'P':
  539.     X            if (setint (flag, optarg, &Pagesize, 1, MAXLINES) == FAILURE)
  540.     X                errcount++;
  541.     X            /* FALLTHROUGH */
  542.     X
  543.     X        case 'p':
  544.     X            Paginate = TRUE;
  545.     X            break;
  546.     X
  547.     X        case 's':
  548.     X            if (setint (flag, optarg, &Spacing, 1, MAXLINES) == FAILURE)
  549.     X                errcount++;
  550.     X            break;
  551.     X
  552.     X        case 't':
  553.     X            if (Ntabs >= MAXTAB)
  554.     X                {
  555.     X                fprintf (stderr, "%s: at most %d -%c options allowed\n",
  556.     X                    Argv0, MAXTAB, flag);
  557.     X                errcount++;
  558.     X                }
  559.     X            else if (setint (flag, optarg, &Tab[Ntabs], 0, MAXLEN) == FAILURE)
  560.     X                errcount++;
  561.     X            else if (Ntabs > 0)
  562.     X                {
  563.     X                if (*optarg == '+')
  564.     X                    Tab[Ntabs] += Tab[Ntabs-1];
  565.     X                else if (Tab[Ntabs] <= Tab[Ntabs-1])
  566.     X                    {
  567.     X                    fprintf (stderr, "%s: -%c values must increase\n",
  568.     X                        Argv0, flag);
  569.     X                    errcount++;
  570.     X                    }
  571.     X                }
  572.     X            if (Tab[Ntabs] >= MAXLEN)
  573.     X                {
  574.     X                fprintf (stderr, "%s: -%c values must be < %d\n",
  575.     X                    Argv0, flag, MAXLEN);
  576.     X                errcount++;
  577.     X                }
  578.     X            Ntabs++;
  579.     X            break;
  580.     X
  581.     X        case 'T':
  582.     X            if (setint (flag, optarg, &Alltabs, 1, MAXLEN) == FAILURE)
  583.     X                errcount++;
  584.     X            break;
  585.     X
  586.     X        case 'u':
  587.     X            Uppercase = TRUE;
  588.     X            break;
  589.     X
  590.     X        case 'U':
  591.     X            usage (stdout);
  592.     X            exit (0);
  593.     X
  594.     X        case 'w':
  595.     X            if (setint (flag, optarg, &Width, 1, MAXLEN) == FAILURE)
  596.     X                errcount++;
  597.     X            break;
  598.     X        }
  599.     X
  600.     X    /* Now check validity of option settings */
  601.     X    if (Tindent < 0 && Indent < (-Tindent))
  602.     X        Indent = (-Tindent);
  603.     X    if (Ntabs > 0 && Alltabs > 0)
  604.     X        {
  605.     X        fprintf (stderr, "%s: can't set individual and all tabs\n", Argv0);
  606.     X        errcount++;
  607.     X        }
  608.     X    if (Center == TRUE && Justify == TRUE)
  609.     X        {
  610.     X        fprintf (stderr, "%s: centering and justifying incompatible\n", Argv0);
  611.     X        errcount++;
  612.     X        }
  613.     X    else if (Breaklines == TRUE && Justify == TRUE)
  614.     X        {
  615.     X        fprintf (stderr, "%s: breaking and justifying incompatible\n", Argv0);
  616.     X        errcount++;
  617.     X        }
  618.     X    if (Ntabs > 0 && Center == TRUE)
  619.     X        {
  620.     X        fprintf (stderr,"%s: centering and setting tabs incompatible\n", Argv0);
  621.     X        errcount++;
  622.     X        }
  623.     X    if ((Ntabs > 0 || Alltabs > 0) && (Justify == TRUE))
  624.     X        {
  625.     X        fprintf (stderr, "%s: tabstops and justifying incompatible\n", Argv0);
  626.     X        errcount++;
  627.     X        }
  628.     X
  629.     X    /* Print an error message and exit or return index to first file name */
  630.     X    if (errcount > 0)
  631.     X        {
  632.     X        fprintf (stderr, "Usage: %s [options] [-] [files]\n", Argv0);
  633.     X        exit (FAILURE);
  634.     X        }
  635.     X    return (optind);
  636.     X    }
  637.     X
  638.     X/*FUNCTION repeat: repeat a character some number of times */
  639.     Xvoid
  640.     Xrepeat (c, n)
  641.     Xint     c;     /* character to print */
  642.     Xint     n;     /* number of times to print c */
  643.     X    {
  644.     X    while (n-- > 0)
  645.     X        putc (c, stdout);
  646.     X    }
  647.     X
  648.     X /*FUNCTION dotab: expand tabs to spaces to tab stops, returns static buffer */
  649.     X/*ALGORITHM
  650.     X    if all tabs are set to the same value (Alltabs > 0)
  651.     X        then advance on a tab to the next tab stop
  652.     X    else if there are individual tabs set (Ntabs > tabno)
  653.     X        then advance (possibly retreat!) to next set tab
  654.     X    else just treat the tab like a space character
  655.     X*/
  656.     Xchar *
  657.     Xdotab (line)
  658.     Xregister    char    *line;
  659.     X    {
  660.     X    static    char outline[MAXLEN];   /* new line will be built here */
  661.     X    register char *lptr;            /* pointer to current position in outline */
  662.     X    register char *nextptr;         /* position of next tab stop */
  663.     X    int     tabno = 0;              /* how many tabs have been processed */
  664.     X
  665.     X    for (lptr = outline; *line != EOS && *line != EOL; line++)
  666.     X        {
  667.     X        if (*line == TAB)
  668.     X            {
  669.     X            if (Alltabs > 0)
  670.     X                nextptr = lptr + Alltabs - ((lptr - outline) % Alltabs);
  671.     X            else if (Ntabs > tabno) /* move to next set tab */
  672.     X                nextptr = outline + Tab[tabno++];
  673.     X            else
  674.     X                nextptr = lptr + 1;
  675.     X            if (lptr < nextptr)
  676.     X                do    {
  677.     X                    *lptr++ = SP;
  678.     X                } while (lptr < nextptr);
  679.     X            else lptr = nextptr;
  680.     X            }
  681.     X        else
  682.     X            *lptr++ = *line;
  683.     X
  684.     X        /* check for line overflow */
  685.     X        if (lptr >= (outline + MAXLEN))
  686.     X            return (NULL);
  687.     X        }
  688.     X
  689.     X    /* end the expanded tab string and return */
  690.     X    *lptr = EOS;
  691.     X    return (outline);
  692.     X    }
  693.     X
  694.     X /*FUNCTION dojustify: even the right margin for all lines */
  695.     X/*ALGORITHM
  696.     X        directly output any indenting spaces (this is not expanded)
  697.     X        trim trailing spaces (don't want to pad at end)
  698.     X        count number of spaces inside line (where extra spaces will be inserted)
  699.     X        distribute the extra spaces needed among the spaces there
  700.     X    Note: if we are not filling (e.g., last line of output before a break)
  701.     X        then we do not justfy the right margin.
  702.     X*/
  703.     Xvoid
  704.     Xdojustify ()
  705.     X    {
  706.     X    register char *line;       /* will point to first non-space char on line */
  707.     X    int     width;             /* width of line to pad - #'s and indent */
  708.     X    register char *lptr;       /* zips through the line */
  709.     X    int     pad;               /* will need to pad with this many spaces */
  710.     X    int     spaces = 0;        /* will be number of embedded spaces in line */
  711.     X    register int i;            /* used inside inner loop */
  712.     X    int     n = 0;
  713.     X
  714.     X    if (Justpos == 0)          /* nothing to justify, so bail out */
  715.     X        return;
  716.     X
  717.     X    /* strip spaces from end of line and end with EOS */
  718.     X    for (lptr = Justbuf+Justpos; lptr>Justbuf && isspace(lptr[-1]); lptr--);
  719.     X    *lptr = EOS;
  720.     X
  721.     X    width = Width - (Numlines == TRUE ? Numwidth : 0);
  722.     X    pad = width - (lptr - Justbuf);
  723.     X
  724.     X    for (line = Justbuf; *line == SP; line++)
  725.     X        putc (*line, stdout);
  726.     X
  727.     X    if (Filling == TRUE && pad > 0) /* might not fill last line of output */
  728.     X        for (lptr = line; *lptr != EOS; lptr++)
  729.     X            if (*lptr == SP)
  730.     X                spaces++;
  731.     X
  732.     X    if (spaces > 0) /* we have places to insert spaces */
  733.     X        {
  734.     X        for (lptr = line; *lptr != EOS; lptr++)
  735.     X            {
  736.     X            if (*lptr == SP)
  737.     X                for (i = 0; i < pad; i++)
  738.     X                    if (++n == spaces)
  739.     X                        {
  740.     X                        putc (SP, stdout);
  741.     X                        n = 0;
  742.     X                        }
  743.     X            putc (*lptr, stdout);
  744.     X            }
  745.     X        }
  746.     X    else /* just output the line */
  747.     X        for (lptr = line; *lptr != EOS; lptr++)
  748.     X            putc (*lptr, stdout);
  749.     X
  750.     X    Justpos = 0; /* reset buffer position for next line */
  751.     X    }
  752.     X
  753.     X /*FUNCTION println: print lines while watching for page boundaries */
  754.     Xvoid
  755.     Xprintln (count)
  756.     Xint     count;    /* how many lines to print (== Spacing) */
  757.     X    {
  758.     X    Curpos = 0;
  759.     X    while (count-- > 0)
  760.     X        {
  761.     X        putc (EOL, stdout);
  762.     X        Pageline++;
  763.     X        if (Paginate == TRUE && ((Pagesize - Pageline) == Footsize))
  764.     X            {
  765.     X            endpage (TRUE);
  766.     X            return;
  767.     X            }
  768.     X        }
  769.     X    }
  770.     X
  771.     X/*FUNCTION beginline: do any needed justification, paginate if requested, */
  772.     X/*  handle line numbering, temp and regular indents based on prev. filling */
  773.     Xvoid
  774.     Xbeginline (filling)
  775.     XBoole    filling;     /* are we filling now? */
  776.     X    {
  777.     X    int     count;
  778.     X    Boole    newfill = (Filling == FALSE && filling == TRUE);
  779.     X
  780.     X    Filling = filling;
  781.     X    if (Justify == TRUE)
  782.     X        dojustify ();
  783.     X    Outline++;
  784.     X    Curpos = 0;
  785.     X
  786.     X    if (Paginate == TRUE && Pageline == 0)
  787.     X        beginpage ();
  788.     X    else if (Outline > 1)
  789.     X        println (Spacing);
  790.     X
  791.     X    if (Numlines == TRUE)
  792.     X        {
  793.     X        char    *ptr = itoa (Outline, Numwidth - 1);
  794.     X        Curpos += Numwidth;
  795.     X        while (*ptr)
  796.     X            putc (*ptr++, stdout);
  797.     X        putc (SP, stdout);
  798.     X        }
  799.     X
  800.     X    count = Indent;
  801.     X    if (newfill == TRUE)
  802.     X        count += Tindent;
  803.     X    Curpos += count;
  804.     X
  805.     X    if (Justify == TRUE)
  806.     X        while (count-- > 0)
  807.     X            justchar (SP);
  808.     X    else
  809.     X        repeat (SP, count);
  810.     X    }
  811.     X
  812.     X /*FUNCTION itoa: integer to ascii conversion */
  813.     Xchar *
  814.     Xitoa (n, pad)
  815.     Xregister int n;    /* the integer to be printed as a string */
  816.     Xint     pad;       /* amount of space to pad number to */
  817.     X    {
  818.     X    static char numbuf[MAXLEN]; /* answer built in here */
  819.     X    register char *nptr;        /* will be pointer to beginning of number */
  820.     X    Boole    negflg = FALSE;     /* is the number a negative value? */
  821.     X
  822.     X    /* static numbuf is initialized to 0's, so numbuf[MAXLEN-1] == EOS */
  823.     X    if (n == 0)
  824.     X        {
  825.     X        nptr = &numbuf[MAXLEN-1];
  826.     X        *--nptr = '0';
  827.     X        }
  828.     X    else
  829.     X        {
  830.     X        if (n < 0)
  831.     X            {
  832.     X            n = (-n);
  833.     X            negflg = TRUE;
  834.     X            }
  835.     X        for (nptr = &numbuf[MAXLEN-1]; n != 0; n /= 10)
  836.     X            *--nptr = (n % 10) + '0';
  837.     X        if (negflg == TRUE)
  838.     X            *--nptr = '-';
  839.     X        }
  840.     X
  841.     X    while (pad > numbuf+MAXLEN-1-nptr)
  842.     X        *--nptr = SP;
  843.     X
  844.     X    return (nptr);
  845.     X    }
  846.     X
  847.     X /*FUNCTION expand: insert file/page for characters in field of 3part title */
  848.     Xchar *
  849.     Xexpand (title, page, file)
  850.     Xregister    char    *title;   /* the title to be expanded */
  851.     Xint     page;                 /* the current page number */
  852.     Xchar    *file;                /* the name of the current file */
  853.     X    {
  854.     X    static     char answer[MAXLEN];  /* title expanded into this buffer */
  855.     X    register     char *aptr;       /* pointer to answer buf */
  856.     X    register    char *ptr;        /* generic string handling pointer */
  857.     X
  858.     X    for (aptr = answer; *title != EOS; title++)
  859.     X        switch (*title)
  860.     X        {
  861.     X        default: 
  862.     X            *aptr++ = *title;
  863.     X            break;
  864.     X
  865.     X        case PAGENUM:
  866.     X            ptr = itoa (page, 0);
  867.     X            while (*ptr != EOS)
  868.     X                *aptr++ = *ptr++;
  869.     X            break;
  870.     X
  871.     X        case FILENAME:
  872.     X            for (ptr = file; *ptr != EOS; ptr++)
  873.     X                *aptr++ = *ptr;
  874.     X            break;
  875.     X        }
  876.     X    *aptr = EOS;
  877.     X    return (answer);
  878.     X    }
  879.     X
  880.     X /*FUNCTION threepart: 3-part title with left/center/right justified fields */
  881.     X/*  any punctuation character can be the title delimiter: see nroff(1) */
  882.     Xchar *
  883.     Xthreepart (title, page, file, width)
  884.     Xchar    *title;   /* the three part title */
  885.     Xint     page;     /* the current page number */
  886.     Xchar    *file;    /* the current file name */
  887.     Xint     width;    /* the current page width */
  888.     X    {
  889.     X    static char answer[MAXLEN];   /* answer stuffed in here */
  890.     X    register char *aptr;          /* pointer to answer buffer */
  891.     X    int     delim;                /* title delimiter character */
  892.     X
  893.     X    title = expand (title, page, file);
  894.     X    if (!ispunct (*title))
  895.     X        return (title);
  896.     X    for (aptr = answer; aptr < answer + width; aptr++)
  897.     X        *aptr = SP;
  898.     X    aptr = answer;
  899.     X    delim = *title++;
  900.     X    while (*title != EOS && *title != delim)
  901.     X        *aptr++ = *title++;
  902.     X    if (*title++ != EOS) /* now do center */
  903.     X        {
  904.     X        char    *lptr = title;
  905.     X        while (*lptr != EOS && *lptr != delim)
  906.     X            lptr++;
  907.     X        aptr = answer + (width - (lptr - title)) / 2;
  908.     X        if (aptr >= answer)
  909.     X            while (*title != EOS && *title != delim)
  910.     X                *aptr++ = *title++;
  911.     X        else
  912.     X            while (*title != EOS && *title != delim)
  913.     X                title++;
  914.     X        if (*title++ != EOS) /* now do left part */
  915.     X            {
  916.     X            char    *eptr = title;
  917.     X            while (*eptr != EOS && *eptr != delim)
  918.     X                eptr++;
  919.     X            eptr--;
  920.     X            aptr = answer + width - 1;
  921.     X            while (eptr >= title)
  922.     X                *aptr-- = *eptr--;
  923.     X            }
  924.     X        }
  925.     X    answer[width] = EOS;
  926.     X    return (answer);
  927.     X    }
  928.     X
  929.     X /*FUNCTION beginpage: handle page header */
  930.     Xvoid
  931.     Xbeginpage ()
  932.     X    {
  933.     X    Pagenum++;
  934.     X    if (Paginate == TRUE && Headsize > 0)
  935.     X        {
  936.     X        int     space1 = Headsize / 2;
  937.     X        int     space2 = Headsize - space1;
  938.     X        char    *optr = threepart (Header, Pagenum, Filename, Width);
  939.     X
  940.     X        repeat (EOL, space1);
  941.     X        while (*optr != EOS)
  942.     X            putc (*optr++, stdout);
  943.     X        repeat (EOL, space2);
  944.     X        Pageline = Headsize;
  945.     X        }
  946.     X    }
  947.     X
  948.     X/*FUNCTION endpage: break filling (output justified text) print footer */
  949.     X/* begin a new page if there is more */
  950.     Xvoid
  951.     Xendpage (more)
  952.     XBoole    more;
  953.     X    {
  954.     X    if (Justify == TRUE)
  955.     X        dojustify ();
  956.     X
  957.     X    if (Paginate == TRUE)
  958.     X        {
  959.     X        int     space1 = Footsize / 2;
  960.     X        int     space2 = Footsize - space1;
  961.     X        char    *optr = threepart (Footer, Pagenum, Filename, Width);
  962.     X
  963.     X        repeat (EOL, Pagesize - Pageline - space2);
  964.     X        while (*optr != EOS)
  965.     X            putc (*optr++, stdout);
  966.     X        repeat (EOL, space2);
  967.     X        Pageline = 0;
  968.     X        }
  969.     X    else if (Curpos > 0 || Indent > 0 || Numlines == TRUE)
  970.     X        putc (EOL, stdout);
  971.     X
  972.     X    if (more == TRUE)
  973.     X        beginpage ();
  974.     X    }
  975.     X
  976.     X /*FUNCTION preprocess: trim space, find blank lines, do titling */
  977.     Xchar *
  978.     Xpreprocess (line)
  979.     Xregister    char    *line;
  980.     X    {
  981.     X    register    char    *lptr;
  982.     X    if (Paginate == TRUE && *line == FF)
  983.     X        {
  984.     X        line++;
  985.     X        endpage (TRUE);
  986.     X        }
  987.     X    if (Delspace == TRUE) /* delete blank space */
  988.     X        {
  989.     X        while (isspace (*line))
  990.     X            line++;
  991.     X        for (lptr = line; *lptr != EOS; lptr++);
  992.     X        while (lptr > line && isspace (*(lptr-1)))
  993.     X            lptr--;
  994.     X        *lptr = EOS;
  995.     X        }
  996.     X    if (Delline == TRUE && (*line == EOL || *line == EOS))
  997.     X        return (NULL);
  998.     X    if (Uppercase == TRUE)
  999.     X        {
  1000.     X        Boole     newword;    /* are we at the start of a new word? */
  1001.     X        for (lptr = line, newword = TRUE; *lptr != EOS; lptr++)
  1002.     X            {
  1003.     X            if (newword && islower (*lptr))
  1004.     X                *lptr = toupper (*lptr);
  1005.     X            newword = !isalnum (*lptr);
  1006.     X            }
  1007.     X        }
  1008.     X    return (line);
  1009.     X    }
  1010.     X
  1011.     X /*FUNCTION dobreak: process broken line & return success status */
  1012.     XStatus
  1013.     Xdobreak (lptr)
  1014.     Xregister char *lptr;    /* line to print out */
  1015.     X    {
  1016.     X    /* break: do tabs, or centering, and then dump the line out */
  1017.     X    beginline (FALSE);
  1018.     X    if (Alltabs > 0 || Ntabs > 0) /* some tabs have been set */
  1019.     X        {
  1020.     X        if ((lptr = dotab (lptr)) == NULL)
  1021.     X            {
  1022.     X            fprintf (stderr, "%s: malformed tab in %s at line %d\n",
  1023.     X                Argv0, Filename, Outline);
  1024.     X            return (FAILURE);
  1025.     X            }
  1026.     X        }
  1027.     X    else if (Center == TRUE && *lptr != EOS && *lptr != EOL)
  1028.     X        repeat (SP, (Width - strlen (lptr)) / 2);
  1029.     X    while (*lptr != EOS && *lptr != EOL)
  1030.     X        putc (*lptr++, stdout);
  1031.     X    Curpos = Width; /* signal end of line */
  1032.     X    return (SUCCESS);
  1033.     X    }
  1034.     X
  1035.     X /*FUNCTION dofill: do line filling */
  1036.     Xvoid
  1037.     Xdofill (lptr)
  1038.     Xregister    char    *lptr;
  1039.     X    {
  1040.     X    register char *eptr;       /* pointer to end of filled words */
  1041.     X    register int wordlen;      /* length of words */
  1042.     X
  1043.     X    while (isspace (*lptr))
  1044.     X        lptr++;
  1045.     X    while (*lptr != EOS) /* fill text by picking up word by word */
  1046.     X        {
  1047.     X        eptr = lptr;
  1048.     X        while (*eptr != EOS && !isspace (*eptr))
  1049.     X            eptr++;
  1050.     X        wordlen = eptr - lptr;
  1051.     X        if ((Outline == 0) || (Curpos + wordlen) >= Width)
  1052.     X            beginline (TRUE);
  1053.     X        else if (Curpos < Width) /* space before word */
  1054.     X            {
  1055.     X            fillchar (SP);
  1056.     X            Curpos++;
  1057.     X            }
  1058.     X        for (Curpos += wordlen; lptr < eptr; lptr++)
  1059.     X            fillchar (*lptr);
  1060.     X        while (isspace (*lptr))
  1061.     X            lptr++;
  1062.     X
  1063.     X        /* extra space at sentence ends (.?!) */
  1064.     X        if ((Curpos < Width) && isendsent (*(eptr-1)) && wordlen > 2)
  1065.     X            {
  1066.     X            fillchar (SP);
  1067.     X            Curpos++;
  1068.     X            }
  1069.     X        }
  1070.     X    }
  1071.     X
  1072.     X /*FUNCTION ff: main formatting routine */
  1073.     X/*
  1074.     X    initializes for each new file, reads lines, trims space and blank lines
  1075.     X    switches between filling and non filling based on breakchars
  1076.     X*/
  1077.     XStatus
  1078.     Xff (file, ioptr)
  1079.     Xchar    *file;    /* file name */
  1080.     XFILE    *ioptr;   /* opened input pointer */
  1081.     X    {
  1082.     X    char    line[BUFSIZ];      /* lines read in here */
  1083.     X    register char *lptr;       /* pointer used to go through line */
  1084.     X    Status    status = SUCCESS;
  1085.     X
  1086.     X    Outline = Pagenum = Pageline = Curpos = 0;
  1087.     X    Filename = file;
  1088.     X    Filling = FALSE;
  1089.     X
  1090.     X    while (fgets (lptr = line, sizeof (line), ioptr))
  1091.     X        {
  1092.     X        if ((lptr = preprocess (lptr)) == NULL)
  1093.     X            continue;
  1094.     X
  1095.     X        if ((Breaklines == TRUE)
  1096.     X        || (*lptr == EOL)
  1097.     X        || (Breakchars ? index (Breakchars, *lptr) != NULL : !isalnum (*lptr)))
  1098.     X            {
  1099.     X            if (dobreak (lptr) == FAILURE)
  1100.     X                return (FAILURE);
  1101.     X            }
  1102.     X
  1103.     X        else
  1104.     X
  1105.     X            dofill (lptr);
  1106.     X        }
  1107.     X
  1108.     X    Filling = FALSE;
  1109.     X    endpage (FALSE);
  1110.     X    return (status);
  1111.     X    }
  1112. SHAR_EOF
  1113. if test 29326 -ne "`wc -c < 'ff.c'`"
  1114. then
  1115.     echo shar: "error transmitting 'ff.c'" '(should have been 29326 characters)'
  1116. fi
  1117. fi
  1118. exit 0
  1119. #    End of shell archive
  1120.  
  1121.